﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Skimpt3.Plugins.Interface;
using System.IO;
using System.Threading;

namespace Skimpt3
{
    public partial class Upload : Form
    {
        public Image img;
        List<IUploaderInterface> Plugins = new List<IUploaderInterface>();
        IUploaderInterface plug;
        string FileName;

        public Upload()
        {
            InitializeComponent();
        }

        private void Upload_Load(object sender, EventArgs e)
        {
            foreach (string Filename in Directory.GetFiles(Path.Combine(Application.StartupPath, "Plugins"), "*.dll"))
            {
                Assembly Asm = Assembly.LoadFile(Filename);
                foreach (Type AsmType in Asm.GetTypes())
                {
                    if (AsmType.GetInterface("IUploaderInterface") != null)
                    {
                        IUploaderInterface Plugin = (IUploaderInterface)Activator.CreateInstance(AsmType);
                        Plugins.Add(Plugin);
                        cmbWhere.Items.Add(Plugin.Name);
                    }
                }
            }

            cmbWhere.SelectedIndex = 0;

            DisableControls();
        }

        [STAThread]
        private void btnUpload_Click(object sender, EventArgs e)
        {
            FileName = txtFileName.Text;
            
            string splitChar = ".";
            char[] splitter = splitChar.ToCharArray();

            plug = Plugins[cmbWhere.SelectedIndex];

            if (FileName.Split(splitter, StringSplitOptions.None).Length < 2)
            {
                FileName += ".jpeg";
            }

            try
            {  
                //Must be set before threading
                plug.FileName = FileName;
                plug.Image = img;

                //Threaded to allow plugins to use dialog boxes
                ThreadStart del = new ThreadStart(plug.Upload);
                Thread thread = new Thread(del);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            if (Plugins[cmbWhere.SelectedIndex].FileUrl != null)
            {
                txtFileUrl.Enabled = true;
                txtFileUrl.Text = Plugins[cmbWhere.SelectedIndex].FileUrl;
            }

            if (Plugins[cmbWhere.SelectedIndex].ThumbUrl != null)
            {
                txtThumb.Enabled = true;
                txtThumb.Text = Plugins[cmbWhere.SelectedIndex].ThumbUrl;
            }
        }

        private void txtFileName_TextChanged(object sender, EventArgs e)
        {
            if (txtFileName.Text != "")
            {
                EnableControls();
            }

            if (txtFileName.Text == "")
            {
                DisableControls();
            }
        }

        private void EnableControls()
        {
            foreach (Control ctrl in groupUploadControls.Controls)
            {
                ctrl.Enabled = true;
            }

            if (cmbWhere.Items.Count == 0)
            {
                cmbWhere.Enabled = false;
                btnUpload.Enabled = false;
            }
        }

        private void DisableControls()
        {
            foreach (Control ctrl in groupUploadControls.Controls)
            {
                ctrl.Enabled = false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult res = folderBrowserDialog1.ShowDialog();

            if (res == DialogResult.OK)
            {
                string path = folderBrowserDialog1.SelectedPath;

                FileName = txtFileName.Text;
                
                string splitChar = ".";
                char[] splitter = splitChar.ToCharArray();

                if (FileName.Split(splitter, StringSplitOptions.None).Length < 2)
                {
                    FileName += ".jpeg";
                }

                img.Save(path + "\\" + FileName);

                MessageBox.Show(FileName + " saved to " + path + "!");
            }
        }

        private void CloseForm(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
